//
//  ViewController.m
//  15-UICollectionViewLifeCycle
//
//  Created by dllo on 16/8/3.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "ViewController.h"
#import "MyCollectionViewCell.h"
#define kCollectionViewReuse @"cellReuse"
#define kArc4RandomColor [UIColor colorWithRed:arc4random() % 256 / 255.0f green:arc4random() % 256 / 255.0f blue:arc4random() % 256 / 255.0f alpha:1]

@interface ViewController () <UICollectionViewDelegate, UICollectionViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((self.view.bounds.size.width - 15) / 2, 120);
    layout.minimumLineSpacing = 5;
    layout.minimumInteritemSpacing = 5;
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
    [collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewReuse];
    // 关于CollectionView LifeCycle
    // 1.numberOfSectionsInCollectionView:collectionView
    // 2.collectionView:collectionView numberOfItemsInSection:section
    // 注意3-5循环执行
    // 3.prepareForReuse(从重用池取出时调用)
    // 4.collectionView:)collectionView willDisplayCell:cell forItemAtIndexPath:indexPath
    // 5.collectionView:collectionView didEndDisplayingCell:cell forItemAtIndexPath:indexPath
    // 当cell完全离开屏幕时, 调用5.collectionView:collectionView didEndDisplayingCell:cell forItemAtIndexPath:indexPath
    // iOS 10以前
    // 出屏幕立即返回, 依旧进入3 -> 4 -> 5
    // iOS10以后
    // 出屏幕立即返回, 会执行4->5, 不会执行3, 提高了效率
}
#pragma mark - CollectionViewLifeCycle
// 区的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    NSLog(@"[%@ : %s]", self, __FUNCTION__);
    return 1;
}
// 区里item的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSLog(@"[%@ : %s]", self, __FUNCTION__);
    return 100;
}
// 从重用池取出cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewReuse forIndexPath:indexPath];
    NSLog(@"%@----%ld, [%@ : %s]", cell, indexPath.row, self, __FUNCTION__);
    return cell;
}
// iOS 8.0以后推出, cell将要显示在屏幕上时
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%@----%ld, [%@ : %s]", cell, indexPath.row, self, __FUNCTION__);
}
// iOS 8.0以后推出, cell已经离开屏幕时
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
     NSLog(@"%@----%ld, [%@ : %s]", cell, indexPath.row, self, __FUNCTION__);
}


@end
